package com.mobile.web.base;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.mobile.util.JwtUtil;
import com.mobile.util.RedisUtil;
import com.mobile.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import java.util.Optional;

public class BaseController {
	protected Logger logger = LogManager.getLogger(this.getClass());

	@SuppressWarnings("unused")
	private static final long serialVersionUID = 6357869213649815390L;

	/** new PageData对象
	 * @return
	 */
	public PageData getPageData(){
		PageData pd = new PageData(this.getRequest());
		String userCode = getUserCode();
		if (null != userCode) {
            pd.put("createUser", userCode);
            pd.put("updateUser", userCode);
        }
		return pd;
	}
	public String getToken() {
		return this.getRequest().getHeader("Access-Token");
	}
	public PageData getPageData(String logInfo){

		PageData pd = new PageData(this.getRequest());
		String userCode = JwtUtil.getClaim(this.getRequest().getHeader("Access-Token"), JwtUtil.ACCOUNT);
		if (null != userCode) {
			pd.put("createUser", userCode);
			pd.put("updateUser", userCode);
			logger.info("[处理日志] " + DateUtil.getSdfTimesForWeb() + "   用户：" + userCode +"   "+logInfo+"操作开始");

		}else {
			logger.info("[处理日志] " + DateUtil.getSdfTimesForWeb() + "   "  +logInfo+"操作开始");
		}
		return pd;
	}

	/**
	 * 得到当前登录用户
	 * @return
	 */
	public PageData getUser() {
		Jedis jedis = RedisUtil.getJedis();
		String user = jedis.hget(Const.SESSION_USER, JwtUtil.getClaim(this.getRequest().getHeader("Access-Token"), JwtUtil.ACCOUNT));
		jedis.close();
		return JSONObject.parseObject(user,PageData.class);
	}



	/**
	 * 得到当前登录用户名
	 * @return
	 */
	public String getUserCode() {
		String token = this.getRequest().getHeader("Access-Token");
		if (Optional.ofNullable(token).isPresent()) {
			return JwtUtil.getClaim(token, JwtUtil.ACCOUNT);
		}
		return null;
	}


	/**得到ModelAndView
	 * @return
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}

	/**得到request对象
	 * @return
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**得到32位的uuid
	 * @return
	 */
	public String get32UUID(){
		return UuidUtil.get32UUID();
	}

	/**得到分页列表的信息
	 * @return
	 */
	public Page getPage(){
		Page page = new Page();
		page.setPd(this.getPageData());
		return page;
	}

	public Page getPage(String logInfo){
		Page page = new Page();
		page.setPd(this.getPageData(logInfo));
		return page;
	}

	public static void logBefore(Logger logger, String interfaceName){
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}

	public static void logAfter(Logger logger){
		logger.info("end");
		logger.info("");
	}
}
